package com.lagou.rabbitmqttl.listener;

import com.lagou.rabbitmqttl.pojo.Order;
import com.rabbitmq.client.Channel;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class RabbitmqListener {

    //订单列表
    public static Map<String, Order> orders = new ConcurrentHashMap<>();

    @RabbitListener(queues = "q.pay.dlx")
    public void dlxListener(@Payload Order order, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) throws IOException {
        Order order1 = orders.get(order.getOrderNum());
        if(order1 == null){
            System.out.println("no order exits");
        }else if(order1.getPayStatus() == Order.UN_PAY){
            order1.setPayStatus(Order.PAY_TIMEOUT);
            orders.put(order1.getOrderNum(),order1);
            System.out.println("订单支付超时，订单信息：" + order1);
        }

        //手动确认
        //channel.basicAck(deliveryTag, false);
    }

}
